/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * WebModelDialog.java
 *
 * Created on Jan 29, 2011, 12:41:16 PM
 */

package net.worlds.component;

import java.util.logging.Level;
import java.util.logging.Logger;
import net.worlds.Main;
import net.worlds.Util;
import net.worlds.WorldsException;
import net.worlds.object.GameObject;
import net.worlds.object.Part;
import net.worlds.object.Texture;
import net.worlds.ui.UI;

/**
 *
 * @author Jacob Morgan
 */
public class AddTextureDialog extends javax.swing.JFrame {
    private GameObject part;
    /** Creates new form WebModelDialog */
    public AddTextureDialog(GameObject part) {
        this.part = part;
        initComponents();
        setLocationRelativeTo(Main.getApplet());
        try
        {
            setIconImages(Util.icons);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        label = new javax.swing.JLabel();
        urlBox = new javax.swing.JTextField();
        cancelButton = new javax.swing.JButton();
        okButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Insert Texture");
        setResizable(false);

        label.setText("URL:");

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        okButton.setText("Insert");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(label)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(urlBox, javax.swing.GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(okButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label)
                    .addComponent(urlBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(okButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        dispose();
        final String url = urlBox.getText();
        if(url.equals(""))
            return;
        try
        {
            for(final GameObject obj : part.getChildren())
            {
                if(obj instanceof Texture)
                    Main.runInMainThread(new Runnable()
                    {
                        @Override
                        public void run() {
                            try {
                                obj.remove();
                            } catch (WorldsException ex) {
                                ex.printStackTrace();
                            }
                        }
                    });
            }
            Main.runInMainThread(new Runnable()
            {
                @Override
                public void run() {
                    try
                    {
                        UI.clearSelection(false);
                        Texture t = (Texture) Main.getGame().getCurrentWorld().createObject("Texture");
                        t.setProp("Image", url, true);
                        t.setParent(part);
                        UI.select(t);
                        UI.updateSelection();
                    }
                    catch(Exception ex)
                    {
                        ex.printStackTrace();
                    }
                }
            });
            dispose();
        }
        catch(RuntimeException ex)
        {
            Main.log("Failed to load texture: "+ex);
        }
    }//GEN-LAST:event_okButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel label;
    private javax.swing.JButton okButton;
    private javax.swing.JTextField urlBox;
    // End of variables declaration//GEN-END:variables

}
